package com.ekuaitu.kuaitu.map;

import android.content.Context;
import android.support.annotation.Nullable;

import com.amap.api.maps.AMap;
import com.amap.api.maps.model.LatLng;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.route.BusRouteResult;
import com.amap.api.services.route.DriveRouteResult;
import com.amap.api.services.route.RideRouteResult;
import com.amap.api.services.route.RouteSearch;
import com.amap.api.services.route.WalkPath;
import com.amap.api.services.route.WalkRouteResult;
import com.ekuaitu.kuaitu.bean.RouteEvent;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by Michael Pi on 2016/9/7.
 */
public class RoutePlanUtils implements RouteSearch.OnRouteSearchListener {
    private Context mContext;
    private static RoutePlanUtils instance;
    private RouteSearch mRouteSearch;
    private LatLonPoint mStartPoint;
    private LatLonPoint mEndPoint;
    private WalkRouteResult mWalkRouteResult;
    private AMap aMap;
    private WalkRouteOverlay mRouteOverLay;
    private WalkPath walkPath;
    private OnRoutePlannedListener onRoutePlannedListener;
    private boolean zoomSpanLarger;

    public static RoutePlanUtils getInstance() {
        if (instance == null) {
            instance = new RoutePlanUtils();
        }
        return instance;
    }

    public RoutePlanUtils() {

    }


    /**
     * @param mContext
     * @param aMap
     * @param curPosition
     * @param tarPosition
     * @param zoomSpanLarger         路线规划后，路线距屏幕边缘的padding大或者小
     * @param onRoutePlannedListener 规划后返回距离，时间
     */
    public void startRoutePlanning(Context mContext, AMap aMap, LatLng curPosition, LatLng tarPosition,
                                   boolean zoomSpanLarger, @Nullable OnRoutePlannedListener onRoutePlannedListener) {
        this.mContext = mContext;
        this.aMap = aMap;
        this.onRoutePlannedListener = onRoutePlannedListener;
        this.zoomSpanLarger = zoomSpanLarger;
        mRouteSearch = new RouteSearch(mContext);
        mRouteSearch.setRouteSearchListener(this);
        mStartPoint = new LatLonPoint(curPosition.latitude, curPosition.longitude);
        mEndPoint = new LatLonPoint(tarPosition.latitude, tarPosition.longitude);
        RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(mStartPoint, mEndPoint);
        RouteSearch.WalkRouteQuery query = new RouteSearch.WalkRouteQuery(fromAndTo, RouteSearch.WalkMultipath);
        mRouteSearch.calculateWalkRouteAsyn(query);     // 异步路径规划步行模式查询
    }


    @Override
    public void onBusRouteSearched(BusRouteResult busRouteResult, int i) {

    }

    @Override
    public void onDriveRouteSearched(DriveRouteResult driveRouteResult, int i) {

    }

    @Override
    public void onWalkRouteSearched(WalkRouteResult result, int errorCode) {
        int dis = -1;
        int dur = -1;
        if (errorCode == 1000) {
            if (result != null && result.getPaths() != null) {
                if (result.getPaths().size() > 0) {
                    mWalkRouteResult = result;
                    if (mRouteOverLay != null) {
                        mRouteOverLay.removeFromMap();
                    }
                    walkPath = mWalkRouteResult.getPaths().get(0);
                    mRouteOverLay = new WalkRouteOverlay(
                            mContext, aMap, walkPath,
                            mWalkRouteResult.getStartPos(),
                            mWalkRouteResult.getTargetPos());
                    mRouteOverLay.removeFromMap();
                    mRouteOverLay.setNodeIconVisibility(false);
                    mRouteOverLay.addToMap();
                    mRouteOverLay.zoomToSpan(zoomSpanLarger);
                    dis = (int) walkPath.getDistance();
                    dur = (int) (walkPath.getDuration() / 60);
                }
            }
        }
        if (onRoutePlannedListener != null) {
            onRoutePlannedListener.onRoutePlannedListener(dis, dur);    //将接收到的结果传递至MainActivity
        }
    }

    @Override
    public void onRideRouteSearched(RideRouteResult rideRouteResult, int i) {

    }

    public void clearRoute() {
        if (mRouteOverLay != null) {
            mRouteOverLay.removeFromMap();
            mRouteOverLay = null;
        }
    }


    public interface OnRoutePlannedListener {
        void onRoutePlannedListener(int distance, int duration);
    }

 /*   public void setOnRoutePlannedListener(OnRoutePlannedListener onRoutePlannedListener) {
        this.onRoutePlannedListener = onRoutePlannedListener;
    }*/

    public boolean isRouteOnMap() {
        return mRouteOverLay != null;
    }

}
